// miniprogram/pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    //本月记账数据统计
    monthBookingData: {
      jieyu: {
        int: 0,
        decimal: '00'
      },

      sr: {
        int: 0,
        decimal: '00'
      },

      zc: {
        int: 0,
        decimal: '00'
      }

    },

    //当月当日的记账数据统计
    currentBooking: {
      date: '',
      count: 0,
      sr: 0,
      zc: 0
    },

    currentBookingData: [],

    start: '',

    end: ''

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 
    this.setData({
      monthBookingData: {
        jieyu: {
          int: 0,
          decimal: '00'
        },
  
        sr: {
          int: 0,
          decimal: '00'
        },
  
        zc: {
          int: 0,
          decimal: '00'
        }
  
      }
    })
    this.getBookingDataByMonth();

    //获取当前日期
    let currentDate = this.getCurrentDate();
    let start = currentDate.split('-');
    start.splice(2, 1, '01');
    
    this.setData({
      start: start.join('-'),
      end: currentDate
    })
    this.getBookingDataByDate(currentDate);
  },

  //获取当前的日期 yyyy-MM-dd
  getCurrentDate() {
     //获取当前日期
     let currentDate = new Date();
     let year = currentDate.getFullYear();
     let month = currentDate.getMonth() + 1;
     month = month >= 10 ? month : '0' + month;
     let date = currentDate.getDate();
     date = date >= 10 ? date : '0' + date;

     return `${year}-${month}-${date}`;
  },

  //获取本月记账数据 当年当月01日 - 当年当月当日
  getBookingDataByMonth() {
    //获取当前日期
    let currentDate = this.getCurrentDate();

    let start = currentDate.split('-');
    start.splice(2, 1, '01');

    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    //调用云端的云函数
    wx.cloud.callFunction({
      //云端的云函数名称
      name: 'get_booking_bymonth',
      data: {
        start: start.join('-'),
        end: currentDate
      }
    }).then(res => {
      //关闭加载提示
      wx.hideLoading();

      
      res.result.data.forEach(v => {
        this.data.monthBookingData[v.mainType.type].int += Number(v.money);
      })

      this.data.monthBookingData.jieyu.int = this.data.monthBookingData.sr.int - this.data.monthBookingData.zc.int;

      for (let key in this.data.monthBookingData) {
        let total = this.data.monthBookingData[key].int.toFixed(2);
        let t = total.split('.');
        this.data.monthBookingData[key].int = t[0];
        this.data.monthBookingData[key].decimal = t[1];
      }

      

      this.setData({
        monthBookingData: this.data.monthBookingData,
        loading: false
      })


    }).catch(err => {
      
    })
  },

  //根据指定日期查询记账数据
  getBookingDataByDate(currentDate) {

    this.setData({
      currentBooking: {
        date: '',
        count: 0,
        sr: 0,
        zc: 0
      }
    })

    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    //调用云端的云函数
    wx.cloud.callFunction({
      //云端的云函数名称
      name: 'get_booking_bydate',
      data: {
        date: currentDate
      }
    }).then(res => {
      //关闭加载提示
      wx.hideLoading();

      // 

      let date = currentDate.split('-');
      
      this.data.currentBooking.count = res.result.data.length;
      this.data.currentBooking.date = `${date[1]}月${date[2]}日`;
      res.result.data.forEach(v => {
        this.data.currentBooking[v.mainType.type] += Number(v.money);
        v.money = Number(v.money).toFixed(2);
      })

      this.data.currentBooking.sr = this.data.currentBooking.sr.toFixed(2);
      this.data.currentBooking.zc = this.data.currentBooking.zc.toFixed(2);

      // res.result.data.sort((a, b) => {
      //   //a - b 升序
      //   //b - a 降序
      //   return new Date(a.date).getTime() - new Date(b.date).getTime();
      // })

      this.setData({
        currentBooking: this.data.currentBooking,
        currentBookingData: res.result.data
      })

    }).catch(err => {
      
    })

  },

  //切换日期
  toggleDate(e) {
    let userDate = e.detail.value.split('-');
    
    let date = `${userDate[1]}月${userDate[2]}日`;
    

    if (date ===  this.data.currentBooking.date) {
      return;
    }

    this.getBookingDataByDate(e.detail.value);

  }


})